package com.songlanyun.config;

import com.songlanyun.interceptor.LoginApiInterceptor;
import com.songlanyun.interceptor.LoginInterceptor;
import com.songlanyun.interceptor.RepeatSubmitInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class InterceptorConfiguration implements WebMvcConfigurer {

    @Value("${ignoreMethod}")
    private String[] ignoreMethod;
    @Value("${ignoreMethodApi}")
    private String[] ignoreMethodApi;
    @Autowired
    LoginInterceptor loginInterceptor;
    @Autowired
    LoginApiInterceptor loginApiInterceptor;

    @Autowired
    RepeatSubmitInterceptor repeatSubmitInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //拦截后台接口
        registry.addInterceptor(loginInterceptor).addPathPatterns("/**")
                .excludePathPatterns(ignoreMethod).excludePathPatterns("/api/**");
        //拦截小程序、APP接口
        registry.addInterceptor(loginApiInterceptor).addPathPatterns("/api/**")
                .excludePathPatterns(ignoreMethodApi);

        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");

    }


    /**
     * 由于项目中配置了mvc，这里需要增加swagger-ui.html静态页面的直接访问
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


}